// pages/userhome/address/address.js
var app = getApp();
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');//引入腾旭地图jssdk
var qqmapsdk;
Page({
  data: {

  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      type: options.type,
      partnerUserId: options.partnerUserId
    })
    wx.request({
      url: app.globalData.url + 'getProductAllTypeList',
      method: 'GET',
      data: {
      },
      success: function (res) {
        that.setData({
          productAllType: res.data.data
        });
      },
    });
     wx.request({
              url: app.globalData.url + 'getCommunityPageData',
              method: 'GET',
              data: {
              },
              success: function (res) {
                that.setData({
                  advanceSale: []
                })
                if (res.data.data.typeList.length > 8) {
                  for (var i = 0; i < 8; i++) {
                    that.data.advanceSale.push(res.data.data.typeList[i])
                  }
                } else {
                  for (var i = 0; i < res.data.data.typeList.length; i++) {
                    that.data.advanceSale.push(res.data.data.typeList[i])
                  }
                }
                that.setData({
                  typeList: that.data.advanceSale,
                  productList: res.data.data.productList
                });
                console.info(res.data.data.typeList);
                if(res.data.result==1){
                  for (var i = 0; i < res.data.data.productList.length; i++) {
                    if (res.data.data.productList[i].promotionMsg !=undefined) {
                      var arr = res.data.data.productList[i].promotionMsg.split(",");
                      res.data.data.productList[i].arr = arr;
                    } else {
                      res.data.data.productList[i].arr = [];
                    }
                    if (res.data.data.productList[i].promotionType != undefined) {
                      //先按照逗号分割字符串
                      let arrycode = res.data.data.productList[i].promotionType.split(",");
                      //去重
                      arrycode = Array.from(new Set(arrycode));
                      //拼接正确的nextOperatorCode
                      res.data.data.productList[i].promotionType = arrycode = arrycode.toLocaleString();
                      // var promotionType = res.data.data[i].promotionType.split(",");
                      // res.data.data[i].promotionType = promotionType;
                    } else {
                      res.data.data.productList[i].promotionType = [];
                    }
                  }
                  
                  that.setData({
                    productList:  res.data.data.productList
                  })
                }
              },
            });
            that.getUserLocation();
            qqmapsdk = new QQMapWX({
              key: '7ZVBZ-NHUC4-VI5UL-DMQJ2-R2RF6-3MBB4'//腾讯地图的key
            });
  },
  onShow: function () {
    var that = this;
    that.setData({
      userId: wx.getStorageSync("userId"),
    })
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
           
          } else {
            // 未授权，跳转到授权页面
            wx.reLaunch({
              url: '/pages/zcuslist/index?partnerUserId=' + that.data.partnerUserId + '&giving=' + that.data.giving + '&usergiftcardid=' + that.data.usergiftcardid + '&price=' + that.data.price + '&cardid=' + that.data.cardid + '&partnerUserName=' + that.data.partnerUserName,
            })
          }
        }
      })

  },
  goNear:function(){
    wx.navigateTo({
      url: '/pages/communityGroupNear/communityGroupNear',
    })
  },
  //定位方法
  getUserLocation: function () {
    var _this = this;
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          //未授权
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                //取消授权
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                });
              } else if (res.confirm) {
                //确定授权，通过wx.openSetting发起授权请求
                wx.openSetting({
                  success: function (res) {
                    if (res.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      _this.geo();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //用户首次进入页面,调用wx.getLocation的API
          _this.geo();
        }
        else {
          //调用wx.getLocation的API
          _this.geo();
        }
      }
    })
  },
  // 获取定位城市
  geo: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res);
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + '&key=7ZVBZ-NHUC4-VI5UL-DMQJ2-R2RF6-3MBB4&get_poi=1',
          data: {},
          header: { 'Content-Type': 'application/json' },
          success: function (ops) {
            that.setData({
              name: ops.data.result.formatted_addresses.recommend,
              city1: ops.data.result.address_component.city,
              district1: ops.data.result.address_component.district,
              latitude: ops.data.result.location.lat,
              longitude: ops.data.result.location.lng,
            })
            wx.request({
              url: app.globalData.url + 'getCommunityHeadListBySite',
              method: 'GET',
              data: {
                site: that.data.longitude + ',' + that.data.latitude,
                userId: that.data.userId,
              },
              header: { 'Content-Type': 'application/json' },
              success: function (res) {
                var data = res.data.data.reverse();
                that.setData({
                  nickname: data[0].nickName,
                  community: data[0].community,
                  communityheadid: data[0].userId,
                  avatar: data[0].avatar,
                });
              },
            });
          },
          fail: function (res){
            wx.showToast({
              title: res.errMsg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  toType: function (e){
    var that = this;
    if (that.data.communityheadid == undefined) {
      wx.showToast({
        title: "请先选择团长",
        icon: 'none'
      })
      return;
    } else {
      wx.navigateTo({
        url: '/pages/communityGroupType/communityGroupType?typeid=' + e.currentTarget.dataset.typeid+'&communityGroup=1&nickname=' + that.data.nickname + '&community=' + that.data.community + '&communityheadid=' + that.data.communityheadid,
      })
    }
  },
  details:function(e){
    var that = this;
    if (that.data.communityheadid == undefined) {
      wx.showToast({
        title: "请先选择团长",
        icon: 'none'
      })
      return;
    } else {
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset.productid + '&communityGroup=1&nickname=' + that.data.nickname + '&community=' + that.data.community + '&communityheadid=' + that.data.communityheadid+"&type=1",
      })
    }
    
  },
})